const connection = require('../app/database');

class FileService {
  async createAvatar(filename, mimeType, size, userId) {
    try {
      const sql = `INSERT INTO avatar (filename,mimeType,size,user_id) VALUES (?,?,?,?)`;
      const [result] = await connection.execute(sql, [filename, mimeType, size, userId]);
      return result;
    } catch (e) {
      console.log(e);
      return e;
    }
  }

  async getAvatar(userId) {
    try {
      const sql = `SELECT * FROM avatar WHERE user_id = ?`;
      const [result] = await connection.execute(sql, [userId]);
      return result;
    } catch (e) {
      console.log(e);
      return e;
    }
  }

  async deleteAvatar(userId) {
    try {
      const sql = `DELETE FROM avatar WHERE user_id = ?`;
      const [result] = await connection.execute(sql, [userId]);
      return result;
    } catch (e) {
      console.log(e);
      return e;
    }
  }
  async createPicture(filename, mimeType, size,momentId, userId) {
    try {
      console.log(filename, mimeType, size,momentId, userId);
      const sql = `INSERT INTO file (filename,mimetype,size,moment_id,user_id) VALUES (?,?,?,?,?)`;
      const [result] = await connection.execute(sql, [filename, mimeType, size, momentId, userId]);
      return result;
    } catch (e) {
      console.log(e);
      return e;
    }
  }
  async getFileInfoByFilename(filename) {
    try {
      const sql = `SELECT * FROM file WHERE filename = ?`;
      const [result] = await connection.execute(sql, [filename]);
      return result[0];
    } catch (e) {
      console.log(e);
      return e;
    }
  }
}
module.exports = new FileService();